package me.gavin.util

import android.widget.ImageView
import androidx.databinding.BindingAdapter
import coil.Coil
import coil.ImageLoader
import coil.load
import me.gavin.R
import me.gavin.di.instanceHolder
import okhttp3.OkHttpClient
import org.koin.core.qualifier.named

object ImageLoader {
    fun init() {
        Coil.setImageLoader(
            ImageLoader.Builder(instanceHolder.get())
                .crossfade(true)
                .okHttpClient(instanceHolder.get<OkHttpClient>(named("images")))
                .build()
        )
    }
}

@BindingAdapter("bindImageUrl", "bindImageHolder", requireAll = false)
fun ImageView.loadUrl(url: String?, holder: Int = R.color.colorAccent) {
    load(url) {
        placeholder(holder)
    }
}